<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="DialogFormVisibleCom" :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="120px"
               style="width: 90%; margin:10px auto;">

        <el-row :gutter="20">
          <el-col :span="12">
            <!-- 所属法人 -->
            <el-form-item label="所属法人" prop="lp_code">
              <el-select v-model="formSubmitDataCom.lp_code" class="filter-item" placeholder="Please select"
                         v-bind:disabled="dialogStatus==='update'">
                <el-option v-for="item in lpOption" :key="item.lp_code" :label="item.lp_name" :value="item.lp_code"/>
              </el-select>
            </el-form-item>
          </el-col>


          <!-- <el-col :span="12">
            <el-form-item label="拟合频率" prop="exct_fqc">
              <el-input v-model="formSubmitDataCom.exct_fqc"/>
            </el-form-item>
          </el-col> -->

          <!-- 拟合频率-分 -->
          <!-- <el-col :span="12">
            <el-form-item label="拟合频率-分">
              <el-select
                v-model="formSubmitDataCom.exct_fqc_minute"
                class="filter-item"
                placeholder=""
                >
                <el-option v-for="item in exct_fqc_minute_options" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col> -->

          <!-- 拟合频率-时 -->
          <!-- <el-col :span="12">
            <el-form-item label="拟合频率-时">
              <el-select
                v-model="formSubmitDataCom.exct_fqc_hour"
                class="filter-item"
                placeholder=""
                >
                <el-option v-for="item in exct_fqc_hour_options" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col> -->

          <!-- 拟合频率-天 -->
          <!-- <el-col :span="12">
            <el-form-item label="拟合频率-天">
              <el-select
                v-model="formSubmitDataCom.exct_fqc_day"
                class="filter-item"
                placeholder=""
                >
                <el-option v-for="item in exct_fqc_day_options" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col> -->

          <!-- 拟合频率-星期 -->
          <!-- <el-col :span="12">
            <el-form-item label="拟合频率-星期">
              <el-select
                v-model="formSubmitDataCom.exct_fqc_week"
                class="filter-item"
                placeholder=""
                >
                <el-option v-for="item in exct_fqc_week_options" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col> -->

          <!-- 拟合频率-月 -->
          <!-- <el-col :span="12">
            <el-form-item label="拟合频率-月">
              <el-select
                v-model="formSubmitDataCom.exct_fqc_month"
                class="filter-item"
                placeholder=""
                >
                <el-option v-for="item in exct_fqc_month_options" :key="item.value" :label="item.label" :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col> -->


          <el-col :span="12">
            <el-form-item label="基础曲线" prop="bs_cv_code">
              <el-select v-model="formSubmitDataCom.bs_cv_code" class="filter-item" placeholder="Please select"
                         v-bind:disabled="dialogStatus==='update'">
                <el-option v-for="item in baseCurveOptions" :key="item.cv_code" :label="item.cv_name"
                           :value="item.cv_code"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="代码类型" prop="pg_type">
              <el-select v-model="formSubmitDataCom.pg_type" class="filter-item" placeholder="Please select"
              >
                <el-option v-for="item in pgTypeOptions" :key="item.code" :label="item.name"
                           :value="item.code"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <!-- 备注 -->
            <el-form-item label="路径+程序名/代码描述" prop="pg_text">
              <el-input type="textarea" v-model="formSubmitDataCom.pg_text" placeholder="Please select"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <!-- 生效日期 -->
            <el-form-item label="生效日期" prop="s_date">
              <el-date-picker
                v-model="formSubmitDataCom.s_date"
                type="date"
                placeholder="Please pick a date"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>

          </el-col>

          <el-col :span="12">
            <!-- 失效日期 -->
            <el-form-item label="失效日期" prop="e_date">
              <el-date-picker
                v-model="formSubmitDataCom.e_date"
                type="date"
                placeholder="Please pick a date"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <!-- 备注 -->
            <el-form-item label="备注：" prop="remark">
              <el-input type="textarea" v-model="formSubmitDataCom.remark" placeholder="Please select"></el-input>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import XEUtils from 'xe-utils'
import exctfqcoptions from '@/views/CurveManageNew/mixins/exctfqcoptions'

export default {
  name: 'submitForm',
  mixins: [exctfqcoptions],
  components: {
    //PageHeaderLayout,
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    formSubmitData: {
      type: Object,
      default: function () {
        return {}
      }
    },
    // 法人selectData
    lpOption: {
      type: Array
    },
    pgTypeOptions: {
      type: Array
    },
    baseCurveOptions: {
      type: Array
    }
  },


  // 定义属性
  data() {
    /* 生效日期验证 */
    var validateDateRangeSdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('生效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.e_date)) && (value > this.formSubmitDataCom.e_date)) {
        callback(new Error('生效日期必须小于失效日期!'));
      } else {
        callback();
      }
    };
    /* 失效日期验证 */
    var validateDateRangeEdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('失效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.s_date)) && (value < this.formSubmitDataCom.s_date)) {
        callback(new Error('失效日期必须大于生效日期!'));
      } else {
        callback();
      }
    };

    return {
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        lp_code: [{required: true, message: '所属法人必填', trigger: 'change'}],
        bs_cv_code: [{required: true, message: '基础曲线必填', trigger: 'change'}],
        pg_type: [{required: true, message: '代码类型必填', trigger: 'change'}],
        pg_text: [{required: true, message: '路径+程序名/代码描述必填', trigger: 'change'}],
        s_date: [{validator: validateDateRangeSdate, required: true, trigger: 'change'}],
        e_date: [{validator: validateDateRangeEdate, required: true, trigger: 'change'}],
        exct_fqc: [{required: true, message: '拟合频率必填', trigger: 'change'}],
      },
      // 表单用到的值
      // calendarTypeOptions,
      // statusOptions: ['published', 'draft', 'deleted'],
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    resetFormData() {
      this.$emit('resetFormDataEmit')
    },
    /* 基础曲线值变化触发事件 */
    bsCvCodeChange(val) {
      this.$emit('bsCvCodeChangeEmit', val)
      console.log('ssss', val);
    },


    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')

        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$delete(this.formSubmitDataCom, "_XID");
          this.$delete(this.formSubmitDataCom, "cv_name");
          const tempData = Object.assign({}, this.formSubmitData)
          this.$emit('handleFromUpdateDataEmit')
        }
      })
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>

</style>
